<?php

namespace App\Http\Controllers\Backend;


use App\Models\User;

class SiteFeedbackController extends BaseController
{
    // 话题
    public function initialize()
    {
        parent::initialize();

        $this->model = 'SiteFeedback';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 返回列表数据
    public function lists()
    {
        $where = $this->listWhere();
        $page  = $this->page;
        $rows  = $this->rows;
        $model = $this->checkModel($this->model);
        if($model === false){
            return $this->error($this->error);
        }

        // 模型 和 查询
        if($where){
            $result = $model->listPage($where, $page, $rows, "id DESC");
        } else{
            $result = $model->isCache(true)->listPage($where, $page, $rows, "id DESC");
        }
        // 用户头像，粉丝数量，关注数量，金币数
        if($result['list']){
            $userDb = new User();
            $uids   = array_column($result['list'], 'uid');
            $users  = $userDb->isCache(true)->getListInIds($uids);
            $users  = array_column($users, null, 'id');
            $types  = $this->userType(); // 认证类型
            foreach($result['list'] as $key => $item){
                $_uid  = $item['uid'];
                $_user = $users[$_uid] ?? [];

                // 整理
                if($_user){
                    // $_user['apply_type'] = $types[$_user['apply_type']]['name'] ?? '';
                    $_user['apply_type'] = $_user['is_apply'] == 1 ? ($types[$_user['apply_type']]['name'] ?? '') : '';
                    $_user['head_img']   = $this->getHeadImg($_user['head_img'] ?? '');
                } else{
                    $_user = (object)[];
                }

                // 整理
                $result['list'][$key]['user'] = $_user;
            }
        }


        return $this->success($result);
    }


    // 反馈
    public function reply()
    {
        $this->throwIf(empty($this->id), '请指定修改的ID编号！');

        $reply = $this->getData('reply', '', 'trim');
        $this->throwIf(empty($reply), '请填写反馈内容！');

        // 组合数据
        $where = ['id' => $this->id];;
        $data   = ['reply' => $reply, 'is_view' => 1,];
        $model  = $this->checkModel($this->model);
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 添加
    protected function _before_add()
    {
        $data = [
            'uid'     => $this->getData('uid', 0, 'intval'),
            'content' => $this->getData('content', null, 'trim'),
            'is_view' => 1,
            'reply'   => $this->getData('reply', null, 'trim'),
        ];

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            'uid'     => $this->getData('uid', 0, 'intval'),
            'content' => $this->getData('content', null, 'trim'),
            'is_view' => 1,
            'reply'   => $this->getData('reply', null, 'trim'),
        ];

        return $data;
    }

}


